package org.i_plus.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.Assistance;
import org.i_plus.model.GiftBean;
import org.i_plus.model.services.GiftService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet implementation class GiftServlet
 */
@WebServlet("/GiftServlet")
@MultipartConfig
public class GiftServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private GiftService service;
	
	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (GiftService)context.getBean("giftService");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接收資料	
		String temp1 = request.getParameter("giftid");
		String name =   request.getParameter("name");
		InputStream temp3 = request.getPart("pic").getInputStream();
		String temp4 =  request.getParameter("pop");
		String temp5 =  request.getParameter("gmoney");
		String temp6 =  request.getParameter("vip");
		String submit =  request.getParameter("submit");

		//驗證資料
		Map<String, String> errors = new HashMap<String, String>();
		request.setAttribute("errors", errors);
		
		if(submit != null && submit.trim().length() != 0){
			if("insert".equals(submit) || "update".equals(submit) || "delete".equals(submit)){
				if(temp1 == null || temp1.trim().length() == 0){
					errors.put("giftid", "please input giftid for" + submit);
				}
			}
		}
		
		if(request.getPart("pic").getSize() != 0){
			String contentType = request.getPart("pic").getContentType();
			int checkimg = contentType.indexOf("image");
			if(checkimg == -1){
				errors.put("pic", "please input an image");
			}
		}
		
		//轉換資料
		int giftid = 0;
		if(temp1 != null && temp1.trim().length() !=0){
			try {
				giftid = Integer.parseInt(temp1);
			} catch (NumberFormatException e) {
				errors.put("giftid", "please input a number");
			}
		}
		int pop = 0;
		if(temp4 != null && temp4.trim().length() != 0){
			try {
				pop = Integer.parseInt(temp4);
			} catch (NumberFormatException e) {
				errors.put("pop", "please input a number");
			}
		}
		int gmoney = 0;
		if(temp5 != null && temp5.trim().length() != 0){
			try {
				gmoney = Integer.parseInt(temp5);
			} catch (NumberFormatException e) {
				errors.put("gmoney", "please input a number");
			}
		}
		int vip = 0;
		if(temp6 != null && temp6.trim().length() !=0){
			try {
				vip = Integer.parseInt(temp6);
			} catch (NumberFormatException e) {
				errors.put("vip", "please input a number");
			}
		}
		byte[] pic = new byte[(int)request.getPart("pic").getSize()];
		temp3.read(pic);
		if(pic.length==0)//如果前端沒有輸入圖片 則用舊的圖
			pic = null;
		if(errors != null && !errors.isEmpty()){
			request.getRequestDispatcher("/ImageView/Gift.jsp").forward(request, response);
			return;
		}
		
		//呼叫model 依照model回傳結果 呼叫view		
		List<GiftBean> result = new ArrayList<GiftBean>();
		List<String> resultpic = new ArrayList<String>();
		
			if("insert".equals(submit)){
				service.insert(giftid, name, pic, pop, gmoney, vip);
			} else if("update".equals(submit)){
				service.update(giftid, name, pic, pop, gmoney, vip);
			} else if("delete".equals(submit)){
				service.delete(giftid);
			} else if("select".equals(submit) && giftid != 0){
				GiftBean temp = service.findByPK(giftid);
				result.add(temp);
				resultpic.add(Assistance.photoByteToImgString("image/jpeg", temp.getPic()));
				request.setAttribute("select", result);
				request.setAttribute("selectpic", resultpic);
			} else{
				result = service.find();
				request.setAttribute("select", result);
				for(GiftBean temp:result)
					resultpic.add(Assistance.photoByteToImgString("image/jpeg", temp.getPic()));
				request.setAttribute("selectpic", resultpic);
			}
		
		request.getRequestDispatcher("/Manage/Gift.jsp").forward(request, response);
		return;
	}
	
}
